package com.longcheer.weibo.vo;

import java.io.Serializable;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import com.longcheer.weibo.util.Utils;
import com.weibo.net.WeiboException;

public class User extends Message implements Serializable {

    private static final long serialVersionUID = -6978587154148813533L;
    
    private long id;                      // 用户id
    private String screenName;            // 微博昵称
    private String name;                  // 友好显示名称，如Bill Gates(此特性暂不支持)
    private int province;                 // 省份编码（参考省份编码表）
    private int city;                     // 城市编码（参考城市编码表）
    private String location;              // 地址
    private String description;           // 个人描述
    private String url;                   // 用户博客地址
    private String profileImageUrl;       // 自定义图像
    private String userDomain;            // 用户个性化URL
    private String gender;                // 性别,m--男，f--女,n--未知
    private int followersCount;           // 粉丝数
    private int friendsCount;             // 关注数
    private int statusesCount;            // 微博数
    private int favouritesCount;          // 收藏数
    private Date createdAt;               // 创建时间
    private boolean following;            // 保留字段,是否已关注(此特性暂不支持)
    private boolean verified;             // 加V标示，是否微博认证用户
    private boolean geoEnabled;           // 地理
    private boolean allowAllActMsg;       // 保留字段（暂时不支持）
    private Status status;                // 用户的最近一条微博信息字段
    
    private String localprofileImagePath; // 本地头像存储路径，如果没有自定义图像，或者没有下载完成，则为null
    
    public User(JSONObject json) throws WeiboException {
        super(json);
        init(json);
    }
    
    private void init(JSONObject json) throws WeiboException {
        if(json!=null){
            try {
                id = json.getLong("id");
                name = json.getString("name");
                screenName = json.getString("screen_name");
                location = json.getString("location");
                description = json.getString("description");
                profileImageUrl = json.getString("profile_image_url");
                url = json.getString("url");
                allowAllActMsg = json.getBoolean("allow_all_act_msg");
                followersCount = json.getInt("followers_count");
                friendsCount = json.getInt("friends_count");
                createdAt = Utils.parseDate(json.getString("created_at"), "EEE MMM dd HH:mm:ss z yyyy");
                favouritesCount = json.getInt("favourites_count");
                following = Utils.getBoolean("following", json);
                verified = Utils.getBoolean("verified", json);
                statusesCount = json.getInt("statuses_count");
                userDomain = json.getString("domain");
                gender = json.getString("gender");
                province = json.getInt("province");
                city = json.getInt("city");
                if (!json.isNull("status")) {
                    status = new Status(json.getJSONObject("status"));
                }
            } catch (JSONException jsone) {
                throw new WeiboException(jsone.getMessage() + ":" + json.toString(), jsone);
            }
        }
    }
    
    @Override
    public MessageType getType() {
        return MessageType.MSG_USER;
    }

    @Override
    public long getId() {
        return id;
    }

    public String getScreenName() {
        return screenName;
    }

    public String getName() {
        return name;
    }

    public int getProvince() {
        return province;
    }

    public int getCity() {
        return city;
    }

    public String getLocation() {
        return location;
    }

    public String getDescription() {
        return description;
    }

    public String getUrl() {
        return url;
    }

    public String getProfileImageUrl() {
        return profileImageUrl;
    }

    public String getUserDomain() {
        return userDomain;
    }

    public String getGender() {
        return gender;
    }

    public int getFollowersCount() {
        return followersCount;
    }

    public int getFriendsCount() {
        return friendsCount;
    }

    public int getStatusesCount() {
        return statusesCount;
    }

    public int getFavouritesCount() {
        return favouritesCount;
    }

    @Override
    public Date getCreatedAt() {
        return createdAt;
    }

    public boolean isFollowing() {
        return following;
    }

    public boolean isVerified() {
        return verified;
    }

    public boolean isGeoEnabled() {
        return geoEnabled;
    }

    public boolean isAllowAllActMsg() {
        return allowAllActMsg;
    }

    public Status getStatus() {
        return status;
    }

    public String getLocalprofileImagePath() {
        return localprofileImagePath;
    }

    public void setLocalprofileImagePath(String localprofileImagePath) {
        this.localprofileImagePath = localprofileImagePath;
    }
}
